home *** CD-ROM | disk | FTP | other *** search
/ Gigantic Games 2 / Gigantic Games 2.iso / pc / _p_ / puzzle / puzzle.c < prev    next >
C/C++ Source or Header  |  1994-12-22  |  5KB  |  233 lines

  1. /*
  2.  *  PUZZLEWINDOW.C
  3.  *
  4.  *  (c)Copyright 1993 by Tobias Ferber,  All Rights Reserved.
  5.  */
  6.  
  7. #include <time.h>
  8.  
  9. #include <exec/types.h>
  10. #include <exec/memory.h>
  11. #include <intuition/intuition.h>
  12. #include <graphics/gfx.h>
  13. #include <graphics/gfxbase.h>
  14. #include <graphics/rastport.h>
  15.  
  16. #include "newlook.h"
  17.  
  18. static char rcs_id[]= "$VER: $Id$";
  19.  
  20. extern struct Window *OpenWindow( struct NewWindow * );
  21. extern UWORD AddGadget( struct Window *, struct Gadget *, UWORD );
  22. extern VOID RefreshGList( struct Gadget *, struct Window *, struct Requester *, UWORD );
  23.  
  24. /* NewWindow Flags and IDCMPFlags */
  25.  
  26. #define NW_FLAGS  (WINDOWCLOSE|WINDOWDEPTH|WINDOWDRAG|ACTIVATE| \
  27.                    SMART_REFRESH|RMBTRAP|GIMMEZEROZERO )
  28.  
  29. #define NW_IDCMP  (CLOSEWINDOW|GADGETUP|VANILLAKEY)
  30.  
  31. static const ULONG PuzzleWindowHandle= 1UL; /* NewLook handle */
  32. static struct Window *PuzzleWindow= (struct Window *)NULL;
  33. static struct Border *PuzzleFrame= (struct Border *)NULL;
  34. static struct Gadget *PuzzleGadgets[16];
  35. static UBYTE PuzzleText[16][3];
  36. static ULONG FreeBrick;
  37.  
  38.  
  39. static void InitPuzzleBricks(void)
  40. {
  41.    static int p[16]= { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 };
  42.    int i,j,k,n;
  43.    time_t t;
  44.  
  45.    for(n=0; n<93; n++)
  46.    {
  47.      time(&t);
  48.      srand(t*270970*rand(42));
  49.      i= (t*rand(t)) & 0x0F;
  50.      j= (i*rand(t)) & 0x0F;
  51.      k= p[i]; p[i]= p[j]; p[j]= k;
  52.    }
  53.  
  54.    for(n=0;n<16;n++)
  55.    {
  56.      if(p[n])
  57.        sprintf(PuzzleText[n],"%d",p[n]);
  58.      else
  59.        sprintf(PuzzleText[FreeBrick=n],"  ");
  60.    }
  61. }
  62.  
  63.  
  64. static void ExchangePuzzleBricks(g0,g1)
  65. struct Gadget *g0, *g1;
  66. {
  67.   USHORT pos0= RemoveGadget(PuzzleWindow,g0);
  68.   USHORT pos1= RemoveGadget(PuzzleWindow,g1);
  69.  
  70.   struct IntuiText *it= g0->GadgetText;
  71.  
  72.   g0->GadgetText= g1->GadgetText;
  73.   g1->GadgetText= it;
  74.  
  75.   if(pos0 != 0xFFFF)
  76.     AddGadget(PuzzleWindow,g0,pos0);
  77.  
  78.   if(pos1 != 0xFFFF)
  79.     AddGadget(PuzzleWindow,g1,pos1);
  80.  
  81.   RefreshGList(g0,PuzzleWindow,NULL,1L);
  82.   RefreshGList(g1,PuzzleWindow,NULL,1L);
  83. }
  84.  
  85.  
  86. static void ClosePuzzleWindow(void)
  87. {
  88.   if(PuzzleWindow)
  89.   {
  90.     CloseWindow(PuzzleWindow);
  91.     PuzzleWindow= (struct Window *)NULL;
  92.   }
  93. }
  94.  
  95.  
  96. static int OpenPuzzleWindow(void)
  97. {
  98.   struct NewWindow *nw;
  99.   short n=0, dx,dy;
  100.  
  101.   for(dy=0; dy<4; dy++)
  102.   {
  103.     for(dx=0; dx<4; dx++)
  104.     {
  105.       PuzzleGadgets[n]= CreateButton(16+26*dx,8+13*dy,26,13,PuzzleText[n],n);
  106.  
  107.       if(!PuzzleGadgets[n])
  108.         return 3;
  109.  
  110.       PuzzleGadgets[n]->GadgetText->DrawMode= JAM2;
  111.  
  112.       if(n>0)
  113.         (PuzzleGadgets[n-1])->NextGadget= PuzzleGadgets[n];
  114.  
  115.       ++n;
  116.     }
  117.   }
  118.  
  119.   /* just to be sure... */
  120.   (PuzzleGadgets[15])->NextGadget= (struct Gadget *)NULL;
  121.  
  122.   if(nw= InitNewWindow(0L,"Puzzle",136,68,NW_IDCMP,NW_FLAGS,PuzzleGadgets[0]))
  123.     PuzzleWindow= OpenWindow(nw);
  124.  
  125.   if(!PuzzleWindow)
  126.     return 2;
  127.  
  128.   dx= PuzzleWindow->BorderLeft + PuzzleWindow->BorderRight + 4;
  129.   dy= PuzzleWindow->BorderTop  + PuzzleWindow->BorderBottom + 2;
  130.  
  131.   PuzzleFrame= CreateFrame(0,0, PuzzleWindow->Width  - dx,
  132.                                 PuzzleWindow->Height - dy, 10,5);
  133.   if(!PuzzleFrame)
  134.     return 1;
  135.  
  136.   DrawBorder(PuzzleWindow->RPort, PuzzleFrame, 2,1);
  137.   RefreshWindowFrame(PuzzleWindow);
  138.   return 0;
  139. }
  140.  
  141.  
  142. static BOOL HandlePuzzleWindowIDCMP(void)
  143. {
  144.   BOOL running= TRUE;
  145.   struct IntuiMessage *imsg;
  146.   ULONG class;
  147.   USHORT code;
  148.   struct Gadget *g;
  149.  
  150.   Wait(1L << PuzzleWindow->UserPort->mp_SigBit);
  151.   while(imsg=(struct IntuiMessage *)GetMsg(PuzzleWindow->UserPort))
  152.   {
  153.     class = imsg->Class;
  154.     code  = imsg->Code;
  155.     g     = (struct Gadget *)imsg->IAddress;
  156.     ReplyMsg(imsg);
  157.  
  158.     switch(class)
  159.     {
  160.       case CLOSEWINDOW:
  161.         running= FALSE;
  162.         break;
  163.  
  164.       case GADGETUP:
  165.         {
  166.           long id= g->GadgetID;
  167.  
  168.           if(id == FreeBrick-4 || id == FreeBrick+4 ||
  169.             (id == FreeBrick-1 && id != 3 && id != 7 && id != 11) ||
  170.             (id == FreeBrick+1 && id != 4 && id != 8 && id != 12) )
  171.           {
  172.              ExchangePuzzleBricks(g,PuzzleGadgets[FreeBrick]);
  173.              FreeBrick= id;
  174.           }
  175.         }
  176.         break;
  177.  
  178.       case VANILLAKEY:
  179.         switch( (UBYTE)code )
  180.         {
  181.           case '\33': /* ESC */
  182.             running= FALSE;
  183.             break;
  184.         }
  185.         break;
  186.     }
  187.   }
  188.   return running;
  189. }
  190.  
  191. /* public */
  192.  
  193. void DoPuzzle(void)
  194. {
  195.   ULONG LastHandle= SetNewLookHandle(PuzzleWindowHandle);
  196.  
  197.   InitPuzzleBricks();
  198.  
  199.   if( OpenPuzzleWindow() == 0)
  200.   {
  201.     while( HandlePuzzleWindowIDCMP() );
  202.  
  203.     ClosePuzzleWindow();
  204.   }
  205.   SmartFreeAll(PuzzleWindowHandle);
  206.   (void)SetNewLookHandle(LastHandle);
  207. }
  208.  
  209.  
  210. #ifdef TEST
  211.  
  212. /* system stuff */
  213. struct IntuitionBase *IntuitionBase;
  214. struct GfxBase *GfxBase;
  215.  
  216. void main(void)
  217. {
  218.   GfxBase= (struct GfxBase *)OpenLibrary("graphics.library",0L);
  219.   IntuitionBase= (struct IntuitionBase *)OpenLibrary("intuition.library",0L);
  220.  
  221.   if(GfxBase && IntuitionBase)
  222.     DoPuzzle();
  223.  
  224.   if(IntuitionBase) CloseLibrary(IntuitionBase);
  225.   if(GfxBase) CloseLibrary(GfxBase);
  226. }
  227.  
  228. #ifdef _DCC /* Dice */
  229. void wbmain(void) { main(); }
  230. #endif /* _DCC */
  231.  
  232. #endif /* TEST */
  233.